package com.mask.socket;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * ClassName TCPClient
 * Create by hx
 * Date 2021/9/21 10:54 上午
 */

public class TCPClient {
    private static final int PORT = 8189;
    public void connect() throws IOException {
        Socket socket = null;
        InputStream inputStream =  null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            InetAddress address = InetAddress.getLocalHost();
            socket = new Socket(address,PORT);
            inputStream = socket.getInputStream();
            int len;
            byte[] buff = new byte[5];
            byteArrayOutputStream = new ByteArrayOutputStream();
            while ((len = inputStream.read(buff)) != -1){
                byteArrayOutputStream.write(buff,0,len);
            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if (byteArrayOutputStream != null){
                byteArrayOutputStream.close();
            }
            if (inputStream != null){
                inputStream.close();
            }
            if (socket != null){
                socket.close();
            }

        }
    }
}
